package hu.village.service.impl;

import hu.village.mapper.AdminMapper;
import hu.village.pojo.Account;
import hu.village.pojo.Admin;
import hu.village.pojo.Village;
import hu.village.service.AdminService;
import hu.village.utils.TimingSetAccountVisible;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Timer;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(String adminName, String password) {
        return adminMapper.selectAdmin(adminName,password);
    }

    @Override
    public List<Village> selectBinding() {
        List<Village> villageList = adminMapper.selectBinding();
        return villageList;
    }

    @Override
    public void process(Integer choice, Village village) {

        if(choice == 0){
           village.setState(0);
           village.setCommittee(null);
        }else {
            village.setState(2);
        }
        adminMapper.updateAccountStateByVillageName(village);
    }

    @Override
    public List<Account> selectCheckingAccount() {
        return adminMapper.selectCheckingAccount();
    }

    @Autowired
    private TimingSetAccountVisible timingSetAccountVisible;
    @Override
    public void processAccount(Integer choice, Account account) {
        if(choice == 0){
            account.setState(0);
        }else {
            account.setState(2);
        }
        adminMapper.updateAccountStateById(account);
//        设置账目在30天过后visible设置为0（不可见）
        timingSetAccountVisible.setId(account.getId());
        Timer timer = new Timer();
        timer.schedule(timingSetAccountVisible,30 * 24 * 60 * 60 * 1000L);
    }

    @Override
    public List<Account> selectReportAccount() {
        return adminMapper.selectReportAccount();
    }

    @Override
    public void processReportedAccount(Integer choice, Account account) {
        if(choice == 1){
            account.setReported(2);

        }else{
            account.setReported(0);
            account.setReportMan(null);
        }
        adminMapper.updateAccountStateByReportMan(account);
    }
}
